package com.lims;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.FileOutputStream;
import java.math.BigInteger;
import java.time.LocalDate;

/**
 * @author limaosheng
 * @create 2024-03-14 15:41
 * @desc
 */
public class 自动生成对应格式的Word文档 {

    public static final String STR = "15\tSaaS\t物管监管端\t居民管理（监管端迁入迁出）\tes改造\t优化\t/\t2222\t33333\t研发自测";


    public static void main(String[] args) throws Exception {
        // 创建一个新的Word文档
        XWPFDocument document = new XWPFDocument();
        addCustomHeadingStyle(document, "标题 1", 1);
        addCustomHeadingStyle(document, "标题 2", 2);
        XWPFParagraph p = document.createParagraph(); // 创建一个段落
        p.setStyle("标题 1");
        LocalDate localDate = LocalDate.now().plusDays(1);
        String date = LocalDateTimeUtil.format(localDate, DatePattern.NORM_DATE_PATTERN);
        date = date.replaceAll("-", ".");
        String title = "施工方案-"+date;
        // 设置标题样式
        XWPFRun run = p.createRun();
        run.setText(title);
        run.setBold(true); // 加粗
        run.setFontFamily("宋体"); // 设置字体为宋体
        // 在文本中添加一个换行符
        run.addCarriageReturn();
        // 设置居中对齐
        p.setAlignment(ParagraphAlignment.CENTER);
        // 设置字号，Apache POI没有直接的方法设置字号，可以通过字体大小间接设置
        // 字号与字体大小存在一定的对应关系，可以通过调整这个值来近似设置字号
        run.setFontSize(16); // 设置字体大小为16
        //一
        oneWord(document);
        //二
        twoWord(document);
        //四
        fourWord(document);
        //五
        fiveWord(document);
        //六
        sixWord(document);
        //七
        elword(document);
        //八
        safeword(document);
        safeword1(document);
        // 将文档写入到磁盘文件
        FileOutputStream out = new FileOutputStream(title+".docx");
        document.write(out);
        out.close();

        System.out.println("Word文档已创建，包含一个格式化的标题。");
    }

    private static void safeword1(XWPFDocument document) {
        String[]  strArr = STR.split("\n");
        for(String s : strArr){
            XWPFParagraph p1 = document.createParagraph();
            XWPFRun p1x = p1.createRun();
            String[] f = s.split("\t");
            p1x.setText("1." + f[0] + " 【" + f[2] + "】" + f[4] + "：若升级失败，可及时回滚，回滚后恢复到升级前生产环境。");
            p1x.setFontSize(11);
            p1x.setFontFamily("宋体"); // 设置字体为宋体
        }
    }

    private static void safeword(XWPFDocument document) {
        XWPFParagraph one = document.createParagraph();
        one.setStyle("标题 1");
        XWPFRun oneRun = one.createRun();
        oneRun.setBold(true);
        oneRun.setFontSize(16);
        oneRun.setText("八、安全风险里面要填的内容");
        oneRun.setFontFamily("宋体"); // 设置字体为宋体
        String[]  strArr = STR.split("\n");
        for(String s : strArr){
            XWPFParagraph p1 = document.createParagraph();
            XWPFRun p1x = p1.createRun();
            String[] f = s.split("\t");
            String fuck = f[9];
            if(StrUtil.isBlank(fuck) || !fuck.contains("自测")){
                fuck = "测试验收";
            }
            p1x.setText("1." + f[0] + " 【" + f[2] + "】" + f[4] + ":" + fuck);
            p1x.setFontSize(11);
            p1x.setFontFamily("宋体"); // 设置字体为宋体
        }
        XWPFParagraph p2= document.createParagraph();
        XWPFRun p2x = p2.createRun();
        p2x.addCarriageReturn();
    }

    private static void elword(XWPFDocument document) {
        XWPFParagraph one = document.createParagraph();
        one.setStyle("标题 1");
        XWPFRun oneRun = one.createRun();
        oneRun.setBold(true);
        oneRun.setFontSize(16);
        oneRun.setText("七、完工验收（标明具体的功能）");
        oneRun.setFontFamily("宋体"); // 设置字体为宋体
        String[]  strArr = STR.split("\n");
        for(String s : strArr){
            String[] f = s.split("\t");
            // 创建一个段落并添加标题
            XWPFParagraph titleParagraph = document.createParagraph();
            titleParagraph.setStyle("标题 2");
            XWPFRun titleRun = titleParagraph.createRun();
            titleRun.setBold(true);
            titleRun.setFontSize(14);
            titleRun.setText("7." + f[0] + " 【" + f[2] + "】" + f[4]);
            titleRun.setFontFamily("宋体"); // 设置字体为宋体

            XWPFParagraph p1 = document.createParagraph();
            p1.setIndentationLeft(480);
            XWPFRun p1x = p1.createRun();
            p1x.setText("运维：11111，确认服务是否正常启动，检查启动日志");
            p1x.setFontSize(11);
            p1x.setFontFamily("宋体"); // 设置字体为宋体
            XWPFParagraph p2= document.createParagraph();
            p2.setIndentationLeft(480);
            XWPFRun p2x = p2.createRun();
            p2x.setText("研发："+f[7]+"，检查服务是否正常启动、功能是否正常");
            p2x.setFontSize(11);
            p2x.setFontFamily("宋体"); // 设置字体为宋体
            XWPFParagraph p3 = document.createParagraph();
            p3.setIndentationLeft(480);
            XWPFRun p3x = p3.createRun();
            p3x.setText("测试：" + f[9]);
            p3x.setFontSize(11);
            p3x.setFontFamily("宋体"); // 设置字体为宋体
            XWPFParagraph p4 = document.createParagraph();
            p4.setIndentationLeft(480);
            XWPFRun p4x = p4.createRun();
            p4x.setText("验收方式：");
            p4x.setFontSize(11);
            p4x.setFontFamily("宋体"); // 设置字体为宋体
            // 在文本中添加一个换行符
            p4x.addCarriageReturn();


        }
    }

    private static void sixWord(XWPFDocument document) {
        XWPFParagraph one = document.createParagraph();
        one.setStyle("标题 1");
        XWPFRun oneRun = one.createRun();
        oneRun.setBold(true);
        oneRun.setFontSize(16);
        oneRun.setText("六、应急和回滚方案");
        oneRun.setFontFamily("宋体"); // 设置字体为宋体
        String[]  strArr = STR.split("\n");
        for(String s : strArr){
            String[] f = s.split("\t");
            // 创建一个段落并添加标题
            XWPFParagraph titleParagraph = document.createParagraph();
            titleParagraph.setStyle("标题 2");
            XWPFRun titleRun = titleParagraph.createRun();
            titleRun.setBold(true);
            titleRun.setFontSize(14);
            titleRun.setText("6." + f[0] + " 【" + f[2] + "】" + f[4]);
            titleRun.setFontFamily("宋体"); // 设置字体为宋体

            XWPFParagraph p1 = document.createParagraph();
            p1.setIndentationLeft(480);
            XWPFRun p1x = p1.createRun();
            p1x.setText("若影响正常业务功能，将采取应急和回滚方案；");
            p1x.setFontSize(11);
            p1x.setFontFamily("宋体"); // 设置字体为宋体
            XWPFParagraph p2= document.createParagraph();
            p2.setIndentationLeft(480);
            XWPFRun p2x = p2.createRun();
            p2x.setText("应急方案：运维工单回滚上一版本，传统环境运维手动回滚上一版本；");
            p2x.setFontSize(11);
            p2x.setFontFamily("宋体"); // 设置字体为宋体
            XWPFParagraph p3 = document.createParagraph();
            p3.setIndentationLeft(480);
            XWPFRun p3x = p3.createRun();
            p3x.setText("回滚预估时间约为 5min；");
            p3x.setFontSize(11);
            p3x.setFontFamily("宋体"); // 设置字体为宋体
            XWPFParagraph p4 = document.createParagraph();
            p4.setIndentationLeft(480);
            XWPFRun p4x = p4.createRun();
            p4x.setText("关联项目：无");
            p4x.setFontSize(11);
            p4x.setFontFamily("宋体"); // 设置字体为宋体
            // 在文本中添加一个换行符
            p4x.addCarriageReturn();
        }
    }

    private static void fiveWord(XWPFDocument document) {
        XWPFParagraph one = document.createParagraph();
        one.setStyle("标题 1");
        XWPFRun oneRun = one.createRun();
        oneRun.setBold(true);
        oneRun.setFontSize(16);
        oneRun.setText("五、施工步骤");
        oneRun.setFontFamily("宋体"); // 设置字体为宋体
        String[]  strArr = STR.split("\n");
        for(String s : strArr){
            String[] f = s.split("\t");
            // 创建一个段落并添加标题
            XWPFParagraph titleParagraph = document.createParagraph();
            titleParagraph.setStyle("标题 2");
            XWPFRun titleRun = titleParagraph.createRun();
            titleRun.setBold(true);
            titleRun.setFontSize(14);
            titleRun.setText("5." + f[0] + " 【" + f[2] + "】" + f[4]);
            titleRun.setFontFamily("宋体"); // 设置字体为宋体

            XWPFParagraph p1 = document.createParagraph();
            p1.setIndentationLeft(480);
            XWPFRun p1x = p1.createRun();
            p1x.setText("1、运维工单滚动升级");
            p1x.setFontSize(11);
            p1x.setFontFamily("宋体"); // 设置字体为宋体

            XWPFParagraph p2 = document.createParagraph();
            p2.setIndentationLeft(480);
            XWPFRun p2x = p2.createRun();
            p2x.setText("2、其它");
            p2x.setFontSize(11);
            p2x.setFontFamily("宋体"); // 设置字体为宋体

            // 在文本中添加一个换行符
            p2x.addCarriageReturn();
        }
    }

    private static void fourWord(XWPFDocument document) {
        XWPFParagraph one = document.createParagraph();
        one.setStyle("标题 1");
        XWPFRun oneRun = one.createRun();
        oneRun.setBold(true);
        oneRun.setFontSize(16);
        oneRun.setText("四、升级平台及升级内容");
        oneRun.setFontFamily("宋体"); // 设置字体为宋体
        String[]  strArr = STR.split("\n");
        for(String s : strArr){
            String[] f = s.split("\t");
            // 创建一个段落并添加标题
            XWPFParagraph titleParagraph = document.createParagraph();
            titleParagraph.setStyle("标题 2");
            XWPFRun titleRun = titleParagraph.createRun();
            titleRun.setBold(true);
            titleRun.setFontSize(14);
            titleRun.setText("4." + f[0] + " 【" + f[2] + "】" + f[4]);
            titleRun.setFontFamily("宋体"); // 设置字体为宋体

            XWPFParagraph p1 = document.createParagraph();
            p1.setIndentationLeft(480);
            XWPFRun p1x = p1.createRun();
            p1x.setText("1、运维工单滚动升级");
            p1x.setFontSize(11);
            p1x.setFontFamily("宋体"); // 设置字体为宋体

            XWPFParagraph p2 = document.createParagraph();
            p2.setIndentationLeft(480);
            XWPFRun p2x = p2.createRun();
            p2x.setText("2、其它");
            p2x.setFontSize(11);
            p2x.setFontFamily("宋体"); // 设置字体为宋体

            // 在文本中添加一个换行符
            p2x.addCarriageReturn();
        }
    }

    private static void twoWord(XWPFDocument document) {
        XWPFParagraph one = document.createParagraph();
        one.setStyle("标题 1");
        XWPFRun oneRun = one.createRun();
        oneRun.setBold(true);
        oneRun.setFontSize(16);
        oneRun.setText("二、施工总体方案");
        oneRun.setFontFamily("宋体"); // 设置字体为宋体
        String[]  strArr = STR.split("\n");
        for(String s : strArr){
            String[] f = s.split("\t");
            // 创建一个段落并添加标题
            XWPFParagraph titleParagraph = document.createParagraph();
            titleParagraph.setStyle("标题 2");
            XWPFRun titleRun = titleParagraph.createRun();
            titleRun.setBold(true);
            titleRun.setFontSize(14);
            titleRun.setText("2." + f[0] + " 【" + f[2] + "】" + f[4]);
            titleRun.setFontFamily("宋体"); // 设置字体为宋体

            XWPFParagraph p1 = document.createParagraph();
            p1.setIndentationLeft(480);
            XWPFRun p1x = p1.createRun();
            p1x.setText("1、运维工单滚动升级");
            p1x.setFontSize(11);
            p1x.setFontFamily("宋体"); // 设置字体为宋体

            XWPFParagraph p2 = document.createParagraph();
            p2.setIndentationLeft(480);
            XWPFRun p2x = p2.createRun();
            p2x.setText("2、其它");
            p2x.setFontSize(11);
            p2x.setFontFamily("宋体"); // 设置字体为宋体

            // 在文本中添加一个换行符
            p2x.addCarriageReturn();
        }
    }

    private static void oneWord(XWPFDocument document) {
        XWPFParagraph one = document.createParagraph();
        one.setStyle("标题 1");
        XWPFRun oneRun = one.createRun();
        oneRun.setBold(true);
        oneRun.setFontSize(16);
        oneRun.setText("一、业务需求说明");
        oneRun.setFontFamily("宋体"); // 设置字体为宋体
        String[]  strArr = STR.split("\n");
        for(String s : strArr){
            String[] f = s.split("\t");
            // 创建一个段落并添加标题
            XWPFParagraph titleParagraph = document.createParagraph();
            titleParagraph.setStyle("标题 2");
            XWPFRun titleRun = titleParagraph.createRun();
            titleRun.setBold(true);
            titleRun.setFontSize(14);
            titleRun.setText("1." + f[0] + " 【" + f[2] + "】" + f[4]);
            titleRun.setFontFamily("宋体"); // 设置字体为宋体

            XWPFParagraph p1 = document.createParagraph();
            p1.setIndentationLeft(480);
            XWPFRun p1x = p1.createRun();
            p1x.setText("需求来源：" + f[6]);
            p1x.setFontSize(11);
            p1x.setFontFamily("宋体"); // 设置字体为宋体

            XWPFParagraph p2 = document.createParagraph();
            p2.setIndentationLeft(480);
            XWPFRun p2x = p2.createRun();
            p2x.setText("升级原因：" + f[4]);
            p2x.setFontSize(11);
            p2x.setFontFamily("宋体"); // 设置字体为宋体

            // 在文本中添加一个换行符
            p2x.addCarriageReturn();
        }
    }



    private static void addCustomHeadingStyle(XWPFDocument docxDocument, String strStyleId, int headingLevel) {
        CTStyle ctStyle = CTStyle.Factory.newInstance();
        ctStyle.setStyleId(strStyleId);
        CTString styleName = CTString.Factory.newInstance();
        styleName.setVal(strStyleId);
        ctStyle.setName(styleName);
        CTDecimalNumber indentNumber = CTDecimalNumber.Factory.newInstance();
        indentNumber.setVal(BigInteger.valueOf(headingLevel));
        // lower number > style is more prominent in the formats bar
        ctStyle.setUiPriority(indentNumber);

        CTOnOff onoffnull = CTOnOff.Factory.newInstance();
        ctStyle.setUnhideWhenUsed(onoffnull);
        // style shows up in the formats bar
        ctStyle.setQFormat(onoffnull);
        // style defines a heading of the given level
        CTPPr ppr = CTPPr.Factory.newInstance();
        ppr.setOutlineLvl(indentNumber);
        ctStyle.setPPr(ppr);
        XWPFStyle style = new XWPFStyle(ctStyle);
        // is a null op if already defined
        XWPFStyles styles = docxDocument.createStyles();
        style.setType(STStyleType.PARAGRAPH);
        styles.addStyle(style);
    }
}
